import qs from "qs"
import { unref, computed, Ref, MaybeRef } from "vue"

export function useAssembledUrl(
  url: MaybeRef<string>,
  ...args: MaybeRef<Record<string, any>>[]
): Ref<string> {
  return computed(() => {
    if (args && args.length > 0) {
      const searchParams = args.reduce(
        (acc: Record<string, any>, curr: MaybeRef<Record<string, any>>) => {
          return Object.assign(acc, unref(curr))
        },
        {},
      )
      return (
        unref(url) +
        "?" +
        qs.stringify(searchParams, {
          indices: false,
          allowDots: true,
        })
      )
    }

    return unref(url)
  })
}
